package com.zhao.fishingguideserver.service.impl;

import com.zhao.fishingguideserver.POJO.Comment;
import com.zhao.fishingguideserver.POJO.CommentLike;
import com.zhao.fishingguideserver.mapper.CommentLikeMapper;
import com.zhao.fishingguideserver.mapper.CommentMapper;
import com.zhao.fishingguideserver.mapper.PostMapper;
import com.zhao.fishingguideserver.service.CommentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Service
public class CommentServiceImpl implements CommentService {

    @Autowired
    private CommentMapper commentMapper;

    @Autowired
    private CommentLikeMapper commentLikeMapper;

    @Autowired
    private PostMapper postMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public String createComment(Comment comment) {
        try {
            // 设置默认值
            comment.setStatus("published");
            comment.setLikeCount(0);
            comment.setCreateTime(new Date());
            
            int result = commentMapper.insert(comment);
            
            if (result > 0) {
                // 更新帖子的评论数
                postMapper.updateCommentCount(comment.getPostId());
                return "评论发表成功";
            } else {
                return "评论发表失败";
            }
        } catch (Exception e) {
            e.printStackTrace();
            return "评论发表失败: " + e.getMessage();
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public String updateComment(Comment comment) {
        try {
            int result = commentMapper.updateComment(comment);
            return result > 0 ? "评论更新成功" : "评论更新失败";
        } catch (Exception e) {
            e.printStackTrace();
            return "评论更新失败: " + e.getMessage();
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public String deleteComment(Integer commentId, Integer userId) {
        try {
            // 验证评论是否存在且属于当前用户
            Comment comment = commentMapper.selectById(commentId);
            if (comment == null) {
                return "评论不存在";
            }
            if (!comment.getUserId().equals(userId)) {
                return "无权限删除此评论";
            }
            
            // 删除评论（软删除，更新状态为deleted）
            comment.setStatus("deleted");
            int result = commentMapper.updateComment(comment);
            
            if (result > 0) {
                // 更新帖子的评论数
                postMapper.updateCommentCount(comment.getPostId());
                return "评论删除成功";
            } else {
                return "评论删除失败";
            }
        } catch (Exception e) {
            e.printStackTrace();
            return "评论删除失败: " + e.getMessage();
        }
    }

    @Override
    public Comment getCommentById(Integer commentId) {
        try {
            return commentMapper.selectById(commentId);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    @Override
    public List<Comment> getCommentsByPostId(Integer postId, Integer page, Integer size) {
        try {
            int offset = (page - 1) * size;
            return commentMapper.selectByPostId(postId, offset, size);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    @Override
    public List<Comment> getCommentsByUserId(Integer userId, Integer page, Integer size) {
        try {
            int offset = (page - 1) * size;
            return commentMapper.selectByUserId(userId, offset, size);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public String toggleLike(Integer commentId, Integer userId) {
        try {
            // 检查是否已点赞
            int likeCount = commentLikeMapper.checkLike(commentId, userId);
            
            if (likeCount > 0) {
                // 已点赞，取消点赞
                commentLikeMapper.deleteByCommentIdAndUserId(commentId, userId);
                commentMapper.updateLikeCount(commentId);
                return "取消点赞成功";
            } else {
                // 未点赞，添加点赞
                CommentLike commentLike = new CommentLike();
                commentLike.setCommentId(commentId);
                commentLike.setUserId(userId);
                commentLike.setCreateTime(new Date());
                
                commentLikeMapper.insert(commentLike);
                commentMapper.updateLikeCount(commentId);
                return "点赞成功";
            }
        } catch (Exception e) {
            e.printStackTrace();
            return "点赞操作失败: " + e.getMessage();
        }
    }

    @Override
    public int getCommentCountByPostId(Integer postId) {
        try {
            return commentMapper.countByPostId(postId);
        } catch (Exception e) {
            e.printStackTrace();
            return 0;
        }
    }

    @Override
    public int getCommentCountByUserId(Integer userId) {
        try {
            return commentMapper.countByUserId(userId);
        } catch (Exception e) {
            e.printStackTrace();
            return 0;
        }
    }
}
